<template>
  <j-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="classifyName" label="分类名称">
          <a-input placeholder="请输入分类名称" v-model="model.classifyName" />
        </a-form-model-item>
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status" label="状态">
          <!-- {{ status }} -->
          <a-switch v-model="status" @change="statusChange()" />
        </a-form-model-item>

        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="iconUrl" label="图标">
          <j-image-upload
            class="avatar-uploader"
            text="图标"
            v-model="model.iconUrl"
            isMultiple
            bizPath="1"
          ></j-image-upload>
        </a-form-model-item>

        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sortIndex" label="排序号">
          <a-input-number v-model="model.sortIndex" />
        </a-form-model-item>
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isOpen" label="是否开放">
          <!-- {{ isOpen }} -->
          <a-switch default-checked v-model="isOpen" @change="isOpenChange()" />
        </a-form-model-item>
        <a-form-model-item
          v-if="title == '添加下级'"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          prop="parentId"
          label="上级id"
        >
          <a-input-number v-model="model.parentId" />
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </j-modal>
</template>

<script>
import { httpAction } from '@/api/manage'

export default {
  name: 'XhdjClassifyNameModal',
  data () {
    return {
      title: '操作',
      visible: false,
      model: {
        // parentId: 0
      },
      status: false,
      isOpen: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },

      confirmLoading: false,
      validatorRules: {},
      url: {
        add: '/server/xhdjClassifyName/add',
        edit: '/server/xhdjClassifyName/edit'
      }
    }
  },
  created () {},
  methods: {
    add () {
      this.edit({})
      this.model.parentId = 0
    },
    edit (record) {
      this.model = Object.assign({}, record)
      this.status = this.isAdminAndstatus(this.model.status)
      this.isOpen = this.isAdminAndstatus(this.model.isOpen)
      console.log('this.model', this.model)
      this.visible = true
    },
    close () {
      this.$emit('close')
      this.visible = false
      this.$refs.form.clearValidate()
    },
    handleOk () {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }

          if (this.model.isOpen) {
            this.model.isOpen = 1
          } else {
            this.model.isOpen = 0
          }
          if (this.model.status) {
            this.model.status = 1
          } else {
            this.model.status = 0
          }
          console.log(7859, this.model)
          httpAction(httpurl, this.model, method)
            .then(res => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        } else {
          return false
        }
      })
    },
    handleCancel () {
      this.close()
    },

    // 状态
    statusChange () {
      if (this.status) {
        this.model.status = 1
      } else {
        this.model.status = 0
      }

      // if (this.model.status) {
      //   this.model.status = true
      // } else {
      //   this.model.status = false
      // }
    },
    // 是否开放
    isOpenChange () {
      if (this.isOpen) {
        this.model.isOpen = 1
      } else {
        this.model.isOpen = 0
      }
      // if (this.model.isOpen) {
      //   this.model.isOpen = true
      // } else {
      //   this.model.isOpen = false
      // }
    },

    // 编辑代理和状态
    isAdminAndstatus (value) {
      if (value) {
        return true
      } else {
        return false
      }
    }
  }
}
</script>

<style lang="less" scoped></style>
